/**
 *Project: jh-framework
 *File: cn.xkshow.framework.aop.UniversalMontitorAspect.java 
 *Author: hardy(admin@xkshow.cn) Copyright
 ****************************************************************
 *   VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *    1.0        2013-7-4         hardy         create
 ****************************************************************
 *note:
 ***************************************************************/ 
package cn.xkshow.framework.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import cn.xkshow.commons.logging.LoggerBase;

@Aspect
public class UniversalMontitorAspect extends LoggerBase {
	
	public UniversalMontitorAspect(){		
	}

	@Pointcut("execution(* *.uni*(..))")   
    public void pointcut(){
    	
    }   
       
    @Before("pointcut()")   
    public void before(){ 
    	this.logger.info("Monitoring:before.");   
    }   
       
    @AfterReturning("pointcut()")   
    public void after(){   
    	this.logger.info("Monitoring:after.");   
    }   
    
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        String clazzString = point.getTarget().getClass().getName();
        String fullPath = clazzString + "." + point.getSignature().getName() + "()";
        int flag = clazzString.indexOf("$");
        if (flag < 0){
        	this.logger.info("Monitoring:start[" + fullPath + "]");
        }
        long time = System.currentTimeMillis();
        Object retVal = point.proceed();
        time = System.currentTimeMillis() - time;
        if (flag < 0){
        	this.logger.info("Monitoring:end[" + fullPath + ", consuming=" + time + "ms]");
        }
        return retVal;
    }

    /*@AfterThrowing(pointcut="sleepPoint()", throwing="ex")
    public void doThrowing(JoinPoint jp, Throwable ex) {
        PropertiesHelper g4PHelper = PropertiesFactory.getPropertiesHelper(PropertyFiles.APP);
        String exceptionMonitor = g4PHelper.getValue("exceptionMonitor");
        if (exceptionMonitor.equals("0")) {
            return;
        }
        String clazzString = jp.getTarget().getClass().getName();
        String methodName = jp.getSignature().getName();
        String fullPath = clazzString + "." + methodName;
        int flag = clazzString.indexOf("$");
        if (flag < 0) {
            log.info("业务处理时发生了异常:[" + fullPath + "]");
            // ex.printStackTrace();
            log.info("[异常类："+clazzString+"],"+"[异常方法："+methodName+"]"+"[异常信息："+ex.getMessage()+"]");
        }    
    }*/
}

